package com.oyjd.framework.util.del;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.oyjd.framework.log.MyLog;

/**
 * 删除URI 或 ICC
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class DelBase {
	private static final String TAG = "DelBase";

	/**
	 * 删除sim卡的特定uri
	 * 
	 * @param context
	 * @param uri
	 */
	public static void delIcc(Context context, Uri uri) {
		try {
			ContentResolver cr = context.getContentResolver();
			Cursor cursor = cr.query(uri, null, null, null, null);
			while (cursor.moveToNext()) {
				String id = cursor.getString(cursor.getColumnIndex("_id"));
				String where = "_id='" + id + "'";
				try {
					cr.delete(uri, where, null);
					MyLog.d(TAG, uri + "->[_id=" + id + "]删除成功!");
				} catch (Exception err) {
					MyLog.e(TAG, uri + "->[_id=" + id + "]删除失败!", err);
				}
			}
			cursor.close();
			MyLog.i(TAG, "ICC完成删除--->" + uri.toString());
		} catch (Exception err) {
			MyLog.e(TAG, "ICC删除失败--->" + uri.toString(), err);
		}
	}

	/**
	 * 删除特定uri数据
	 * 
	 * @param activity
	 */
	public static void delUri(Context context, Uri uri) {
		try {
			ContentResolver cr = context.getContentResolver();
			cr.delete(uri, null, null);
			MyLog.i(TAG, "删除成功--->" + uri.toString());
		} catch (Exception err) {
			MyLog.e(TAG, "删除失败--->" + uri.toString(), err);
		}
	}

	/**
	 * 打印查看uri对应的数据库结构
	 * 
	 * @param context
	 * @param uri
	 */
	public static void printStruct(Context context, Uri uri) {
		ContentResolver cr = context.getContentResolver();
		Cursor cursor = cr.query(uri, null, null, null, null);
		if (cursor != null) {
			String[] columns = cursor.getColumnNames();
			for (int i = 0; i < columns.length; i++) {
				String key = cursor.getColumnName(i);
				MyLog.d(TAG, "col-----" + key + "----" + cursor.getColumnIndex(key));
			}
			MyLog.d(TAG, "=======================");
			while (cursor.moveToNext()) {
				MyLog.d(TAG, columns[0] + "->" + cursor.getInt(0) + " " + columns[1] + "->" + cursor.getString(1));
			}
			cursor.close();
		}
	}
}
